#!/bin/bash

CI_COMMIT_SHA=${GIT_COMMIT:=$(git log -n 1 --pretty=format:'%h')}
IMAGE_TAG="$CI_COMMIT_SHA"
DOCKER_HUB=10.3.0.171

build(){
        mvn package -Dmaven.test.skip=true
        docker build -t $DOCKER_HUB/260219/discovery-server:$IMAGE_TAG ./discovery-server
        docker build -t $DOCKER_HUB/260219/discovery-client:$IMAGE_TAG ./discovery-client
        docker build -t $DOCKER_HUB/260219/nacos-config-client:$IMAGE_TAG ./nacos-config-client
        docker build -t $DOCKER_HUB/260219/spring-cloud-gateway:$IMAGE_TAG ./spring-gateway

        docker login --username=$DOCKER_USER --password=$DOCKER_PASSWORD $DOCKER_HUB
        echo "push到镜像中心"
        echo $IMAGE_TAG
        echo $DOCKER_HUB
        docker push  $DOCKER_HUB/260219/discovery-server:$IMAGE_TAG
        docker push  $DOCKER_HUB/260219/discovery-client:$IMAGE_TAG
        docker push  $DOCKER_HUB/260219/nacos-config-client:$IMAGE_TAG
        docker push  $DOCKER_HUB/260219/spring-cloud-gateway:$IMAGE_TAG

        docker rmi $DOCKER_HUB/260219/discovery-server:$IMAGE_TAG
        docker rmi $DOCKER_HUB/260219/discovery-client:$IMAGE_TAG
        docker rmi $DOCKER_HUB/260219/nacos-config-client:$IMAGE_TAG
        docker rmi $DOCKER_HUB/260219/spring-cloud-gateway:$IMAGE_TAG
}
deploy_test(){
        sed -i "s/{{TAG}}/${IMAGE_TAG}/g" ./discovery-server/discovery-server-deployment.yaml
        sed -i "s/{{TAG}}/${IMAGE_TAG}/g" ./discovery-client/discovery-client-deployment.yaml
        sed -i "s/{{TAG}}/${IMAGE_TAG}/g" ./nacos-config-client/nacos-config-client-deployment.yaml
        sed -i "s/{{TAG}}/${IMAGE_TAG}/g" ./spring-gateway/spring-cloud-gateway-deployment.yaml

        echo "发布镜像到k8s上"
        kubectl apply -f ./discovery-server/discovery-server-deployment.yaml
        kubectl apply -f ./discovery-client/discovery-client-deployment.yaml
        kubectl apply -f ./nacos-config-client/nacos-config-client-deployment.yaml
        kubectl apply -f ./spring-gateway/spring-cloud-gateway-deployment.yaml

}


#根据输入参数，选择执行对应方法，不输入则执行使用说明
case $1 in
 build|deploy_test )
   $1;;
 * )
   echo "not support!!! example: go <test|build|deploy>"
   exit 1;;
esac
